/**
This file is part of Gel2DE.

    Gel2DE is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gel2DE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Gel2DE.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (C) 2012, University of Bergen
*/

#ifndef gel2de_File_h
#define gel2de_File_h

#include <vector>
#include <string>

namespace gel2de {

   class FileFilter;

   /// A representation of files and directories.
   /// Resembling java.io.File.
   class File
   {
   public:
      static const std::string expandPath(const std::string& input);
      
      File(const std::string& path);
      File(const File& parent, const std::string& name);

      /// Returns true if this file is a directory.
      bool isDirectory() const;

      bool hasParent() const { return m_path != "/"; }

      /// Returns true if this file exists.
      bool exists() const;

      /// Adds all files in this directory to a list.
      /// Files "." and ".." are excluded.
      void listFiles(std::vector<File>& files,
                     FileFilter* fileFilter = NULL) const;

      std::string name() const;
      std::string path() const { return m_path; }

      File parent() const;

   private:
      void removeTrailingSlash();
      std::string m_path;
   };

   /// Comparance of two files.
   inline bool operator < (const File& a, const File& b) {
      return a.path() < b.path();
   }

   /// A filter for file names.
   /// Resembling java.io.FileFilter.
   class FileFilter
   {
   public:
      virtual ~FileFilter();

      virtual bool accept(const File& file) = 0;
   };

   /// A FileFilter which accepts files with specified suffixes.
   class SuffixFileFilter : public FileFilter
   {
   public:
      virtual ~SuffixFileFilter();

      virtual bool accept(const File& file);

      void addSuffix(const std::string& suffix) {
         m_suffixes.push_back(suffix); }

   private:
      std::vector<std::string> m_suffixes;
   };

} // gel2de::

#endif // gel2de_File_h
